
#include "qyFilePathUtil.h"
#include "../lex/qyLex.h"
#ifdef _WIN32
#include <windows.h>
#endif

//-----------------------------------------------------------------------
qyString qy_UtilGetFileExtension( const qyString &filePath )
{
    boost::char_separator<char> tokenSep( "./\\" );
    qyTokenizer tokenizer( filePath, tokenSep );
    qyTokenIterator end = tokenizer.end();
    qyTokenIterator ext, it;
    for ( it = tokenizer.begin(); it != end; ++it )
    {
        ext = it;
    }
	char buf[512];
	strcpy( buf, (*ext).c_str() );
	qyString file( buf );
    return file;
}

//-----------------------------------------------------------------------
qyString qy_UtilGetFilePathWithoutExt( const qyString &filePath )
{
    qyString ext = qy_UtilGetFileExtension( filePath );
    int length = ext.length() + 1;
    char filebuffer[1024];
    memcpy( filebuffer, filePath.c_str(), filePath.length() - length );
    filebuffer[filePath.length() - length] = '\0';
    return qyString( filebuffer );
}

//-----------------------------------------------------------------------
qyString qy_UtilGetFileNameFromPath( const qyString &filePath )
{
    boost::char_separator<char> tokenSep( "/\\" );
    qyTokenizer tokenizer( filePath, tokenSep );
    qyTokenIterator end = tokenizer.end();
    qyTokenIterator ext, it;
    for ( it = tokenizer.begin(); it != end; ++it )
    {
        ext = it;
    }
    return (*ext);
}

//-----------------------------------------------------------------------
qyString qy_UtilGetDirectoryFromFilePath( const qyString &filePath )
{
    qyString ext = qy_UtilGetFileNameFromPath( filePath );
    int length = ext.length() + 1;
    char filebuffer[1024];
    memcpy( filebuffer, filePath.c_str(), filePath.length() - length );
    filebuffer[filePath.length() - length] = '\0';
    return qyString( filebuffer );
}

//-----------------------------------------------------------------------
void qy_UtilCreateFilePath( const qyString &filePath )
{
#ifdef _WIN32
    bool result = ::CreateDirectoryA( filePath.c_str(), NULL );
#endif
}
